package com.boc.langxiang.presenter.shop;

import com.boc.langxiang.contract.shop.ShoppingCarContract;
import com.boc.langxiang.entity.request.ShoppingCarListRequest;
import com.boc.langxiang.entity.response.BaseResponse;
import com.boc.langxiang.entity.response.ShoppingCarContent;
import com.boc.langxiang.manager.UserInfoManager;
import com.boc.langxiang.net.HandleErrorSubscriber;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by Li Guoxi on 2016/6/30.
 */
public class ShoppingCarListPresenter extends SuperPresenter implements ShoppingCarContract.Presenter {

    ShoppingCarContract.View mView;

    public ShoppingCarListPresenter(ShoppingCarContract.View mView) {
        super(mView);
        this.mView = mView;
    }

    @Override
    public void getList() {
        if (UserInfoManager.getInstance().getToken() == null) {
            mView.onError(null, "您尚未登录,请登录后查询购物车信息");
            return;
        }
        mView.showLoading();
        ShoppingCarListRequest request = new ShoppingCarListRequest();
        mService.getShoppingCarList(request.params())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new HandleErrorSubscriber<BaseResponse<ShoppingCarContent>>(mView) {
                    @Override
                    public void onSuccess(BaseResponse<ShoppingCarContent> response) {
                        mView.hideLoading();
                        mView.getListSuccess(response);
                    }
                });
    }
}
